package com.app.controller.backstage;
import com.app.model.AdminUserModel;
import com.app.model.JobFairSceneModel;
import com.app.model.NewsModel;
import com.app.service.AdminUserService;
import com.app.service.JobFairSceneService;
import com.app.util.CommonHelp;
import com.app.util.Result;
import com.app.util.TokenUtils;
import com.app.vo.JobFairSceneVo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
@RestController
@Controller
@NoArgsConstructor
@AllArgsConstructor
@Data
public class JobFairSceneBkController {


    @Autowired
    public JobFairSceneService jobFairSceneService;

    @Autowired
    AdminUserService adminUserService;


    @GetMapping("bakGetJobFairList")//后台查招聘会列表
    public Result<Object> getNewsList(HttpServletRequest request,
                                      @RequestParam(required = false) String keyWord,
                                      @RequestParam(required = false) String startTime,
                                      @RequestParam(required = false) String endTime,
                                      int page,
                                      int limit,
                                      String sceneType) {
        String token = request.getHeader("token");
        String userId = TokenUtils.nothingness(token);
        if (userId == null) {
            return Result.error(405, "token不存在");
        }

        AdminUserModel user = adminUserService.getUserById(Integer.parseInt(userId));
        if (user == null) {
            return Result.error(405, "用户不存在");
        }
        JobFairSceneModel model = new JobFairSceneModel();
        model.setPage(page);
        model.setLimit(limit);
        model.setKeyWord(keyWord);
        model.setStartTime(startTime);
        model.setEndTime(endTime);
        if (sceneType != null) {
            model.setSceneType(sceneType);
        }
        JobFairSceneVo vo = jobFairSceneService.bakGetJobFairList(model);
        return Result.success(vo);
    }
    @GetMapping("/bakGetJobFairById")
    public Result<Object> bakGetJobFairById(HttpServletRequest request,
                                    @RequestParam("id") int id){
        String token = request.getHeader("token");
        String userId = TokenUtils.nothingness(token);
        if (userId == null) {
            return Result.error(405, "token不存在");
        }

        AdminUserModel user = adminUserService.getUserById(Integer.parseInt(userId));
        if (user == null) {
            return Result.error(405, "用户不存在");
        }
        JobFairSceneModel model = jobFairSceneService.getJobFairById(id);

        return Result.success(model);
    }

    @RequestMapping(value = "insertAndUpdateJobFair", method= RequestMethod.POST)
    public Result<Object> insertAndUpdateJobFair(HttpServletRequest request,
                                                 @RequestBody JobFairSceneModel model
    ){
        String token = request.getHeader("token");
        String userId = TokenUtils.nothingness(token);
        if (userId == null) {
            return Result.error(405, "token不存在");
        }

        AdminUserModel user = adminUserService.getUserById(Integer.parseInt(userId));
        if (user == null) {
            return Result.error(405, "用户不存在");
        }
        model.setState(1);
        model.setDeleteState(1);
        if(model.getId()==0){
            model.setCrtTime(String.valueOf(CommonHelp.getNowTimestamp()));
            jobFairSceneService.insert(model);
        }else {
            jobFairSceneService.update(model);
        }
        return Result.success("操作成功");
    }

    @GetMapping("/deleteJobFairById")//假删除
    public Result<Object> deleteById(HttpServletRequest request,
                             @RequestParam("id") int id,
                             @RequestParam("state") int state
    ){
        String token = request.getHeader("token");
        String userId = TokenUtils.nothingness(token);
        if (userId == null) {
            return Result.error(405, "token不存在");
        }

        AdminUserModel user = adminUserService.getUserById(Integer.parseInt(userId));
        if (user == null) {
            return Result.error(405, "用户不存在");
        }
        JobFairSceneModel model = jobFairSceneService.getJobFairById(id);
        if(model==null){
            return Result.error(500, "空值或者传参错误");
        }
        model.setDeleteState(state);
        jobFairSceneService.update(model);
        return Result.success("操作成功");
    }

    @GetMapping("/editState")
    public Result<Object> editState(HttpServletRequest request,
                            @RequestParam("id") int id,
                            @RequestParam("state") int state
    ){
        String token = request.getHeader("token");
        String userId = TokenUtils.nothingness(token);
        if (userId == null) {
            return Result.error(405, "token不存在");
        }

        AdminUserModel user = adminUserService.getUserById(Integer.parseInt(userId));
        if (user == null) {
            return Result.error(405, "用户不存在");
        }
        JobFairSceneModel model = jobFairSceneService.getJobFairById(id);
        if(model==null){
            return Result.error(500, "空值或者传参错误");
        }

        model.setState(state);
        jobFairSceneService.update(model);
        return Result.success("操作成功");
    }


}
